package com.wondersgroup.fjzlpt.org.dao.impl.jgqy.qysq;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqKsxxDAO;
@Repository
public class QysqKsxxDAOImpl extends BaseJTDAOImpl implements QysqKsxxDAO{
	@Override
	@Autowired
	@Qualifier("TB_JGQY_YLT_KSXX")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}
	
	@Autowired
	@Qualifier("TB_ZD_KSXX")
	TableBean ksTableBean;

	/**
	 * 根据医联体id删除记录 
	 * @param map
	 */
	public void delete(Map<String, String> map) {
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("delete from " + this.tb.getTableName() + " where YLTID = ? ");
		List list = new ArrayList();
		list.add(map.get("YLTID"));
		String value = map.get("YLJGIDS");
		if(!StringUtils.isEmpty(value)){
			sbuf.append(" and YLJGID in (");
			String[] strs = value.split(",");
			String params = "";
			for (String str : strs) {
				params += "?,";
				list.add(str);
			}
			sbuf.append(params.substring(0, params.lastIndexOf(",")));
		}
		value = map.get("ZDYLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(",?");
			list.add(value);
		}
		sbuf.append(")");
		SQLBean sb = new SQLBean();
		sb.setSql(sbuf.toString());
		sb.setParamAry(list);
		super.update(sb);
	}
	
	/**
	 * 根据医联体id查询记录 
	 * @param map
	 */
	public JSONArray query(Map<String, String> map) {
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("select a.*,");
		sbuf.append("(select KSMC from "+ this.ksTableBean.getTableName() +" where PTJGID = a.YLJGID and KSDM = a.KSDM) KSMC");
		sbuf.append(" from "+ this.tb.getTableName());
		sbuf.append(" a where 1=1 ");
		List list = new ArrayList();
		String value = map.get("YLTID");
		if(!StringUtils.isEmpty(value)){
			sbuf.append(" and a.YLTID = ?");
			list.add(value);
		}
		value = map.get("YLJGID");
		if(!StringUtils.isEmpty(value)){
			sbuf.append(" and a.YLJGID = ?");
			list.add(value);
		}
		value = map.get("GLID");
		if(!StringUtils.isEmpty(value)){
			sbuf.append(" and a.GLID = ?");
			list.add(value);
		}
		SQLBean sb = new SQLBean();
		sb.setSql(sbuf.toString());
		sb.setParamAry(list);
		return super.queryList(sb);
	}
	
}
